package com.snow.useviewdemo.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import org.jetbrains.annotations.NotNull;

/**
 * Created by zhyt
 * Date: 2022/1/8
 * Describe:
 */
public class NumberFlipView extends AppCompatTextView {

    private int duration = 1500;
    private float number;

    public NumberFlipView(@NonNull @NotNull Context context) {
        super(context);
    }

    public NumberFlipView(@NonNull @NotNull Context context, @Nullable @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public float getNumber() {
        return number;
    }

    public void setNumber(float number) {
        this.number = number;
        setText(String.format("%,.0f", number));
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    /**
     * 显示
     *
     * @param number
     */
    public void runWithAnimation(float number) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
                this, "number", 0, number);
        objectAnimator.setDuration(duration);
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.start();
    }
}